package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.LinDao;
import com.hdaccp.xingmu.dao.SrecordDao;
import com.hdaccp.xingmu.entity.House;
import com.hdaccp.xingmu.entity.Lin;
import com.hdaccp.xingmu.entity.Srecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/srecord")
public class SrecordCon {
    @Autowired
    private SrecordDao srecordDao;
    @Autowired
    private LinDao linDao;
    @PersistenceContext
    private EntityManager entityManager;

    @RequestMapping("/tian")
    public void tian(){

    }

    @RequestMapping("/add")
    @ResponseBody
    public void tian(Srecord s){
        Lin l = new Lin();
        l.setLasttime(s.getLasttime());
        l.setSdate(s.getSdate());
        l.setBdate(s.getBdate());
        l.setJdate(s.getJdate());
        l.setHouse(s.getHouse());
        l.setCostitem(s.getCostitem());
        l.setNum(s.getId());
        l.setZmoney(s.getZmoney());
        l.setId(s.getId());
        linDao.save(l);

        Srecord s1 = new Srecord();

        s1.setLasttime(s.getLasttime());
        s1.setSdate(s.getSdate());
        s1.setBdate(s.getBdate());
        s1.setJdate(s.getJdate());
        s1.setOrderhao(s.getOrderhao());
        s1.setHouse(s.getHouse());
        s1.setCostitem(s.getCostitem());
        s1.setNum(s.getId());
        s1.setZmoney(s.getZmoney());
        s1.setType(s.getType());

        srecordDao.save(s1);
    }

    @RequestMapping("/add2")
    @ResponseBody
    public void tian2(Srecord s){
        Lin l = new Lin();
        l.setSdate(s.getSdate());
        l.setBdate(s.getBdate());
        l.setJdate(s.getJdate());
        l.setHouse(s.getHouse());
        l.setCostitem(s.getCostitem());
        l.setNum(s.getId());
        l.setZmoney(s.getZmoney());
        l.setId(s.getId());


        linDao.save(l);

        Srecord s1 = new Srecord();

        s1.setSdate(s.getSdate());
        s1.setBdate(s.getBdate());
        s1.setJdate(s.getJdate());
        s1.setOrderhao(s.getOrderhao());
        s1.setHouse(s.getHouse());
        s1.setNum(s.getId());
        s1.setZmoney(s.getZmoney());
        s1.setType(s.getType());

        srecordDao.save(s1);
    }

    @RequestMapping("/cc")
    @ResponseBody
    public List<Srecord> list(String orderid){

        Specification specification = new Specification<Srecord>(){
            @Override
            public Predicate toPredicate(Root<Srecord> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                    predicates.add(criteriaBuilder.like(root.get("orderhao").get("orderid"),"%"+orderid+"%"));
                Predicate[] p = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(p));
            }
        };
        List<Srecord> all2 = srecordDao.findAll(specification);
       return all2;
    }

    @RequestMapping("/cc2")
    @ResponseBody
    public List<Srecord> list2(String orderid,int type){
        System.out.println("orderid*******************"+orderid);
        System.out.println("type**********************"+type);
        Specification specification = new Specification<Srecord>(){
            @Override
            public Predicate toPredicate(Root<Srecord> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                predicates.add(criteriaBuilder.equal(root.get("house").get("id"),orderid));
                if(type!=-1){
                    predicates.add(criteriaBuilder.equal(root.get("type"),type));
                }
                Predicate[] p = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(p));
            }
        };
        List<Srecord> all2 = srecordDao.findAll(specification);
        return all2;
    }

    @RequestMapping("/upd")
    @ResponseBody
    public Srecord upd(long id){
        Srecord srecord = srecordDao.findById(id).get();
        srecord.setType(2);
       return  srecordDao.save(srecord);
    }

}
